#include "kernel_operator.h"
constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

class KernelHardShrink {
public:
    __aicore__ inline KernelHardShrink() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, const HardShrinkCustomTilingData &tiling_data)
    {
        this->tiling = tiling_data; 
        this->blockLength = tiling.totalLength / AscendC::GetBlockNum();
        this->tileNum = tiling.tileNum;
        this->tileLength = this->blockLength / tiling.tileNum / BUFFER_NUM;

        xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        zGm.SetGlobalBuffer((__gm__ DTYPE_Z *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Z));
        pipe.InitBuffer(tmpBuffer0, this->tileLength * sizeof(DTYPE_X));
        maskSize = (this->tileLength + 7 ) / 8;
        pipe.InitBuffer(tmpBuffer1, maskSize * sizeof(uint8_t));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
        AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
        AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.AllocTensor<DTYPE_Z>();
        AscendC::LocalTensor<DTYPE_X> tmpLocal = tmpBuffer0.Get<DTYPE_X>();
        AscendC::LocalTensor<uint8_t> maskLocal = tmpBuffer1.Get<uint8_t>();
        // 创建比较掩码：|x| > λ
        AscendC::Abs(tmpLocal, xLocal, this->tileLength);          // |x|
        AscendC::CompareScalar(maskLocal, tmpLocal, (DTYPE_X)tiling.threshold, AscendC::CMPMODE::GT, this->tileLength);  // |x| > λ
        // 根据掩码选择：如果 |x| > λ 则选择 x，否则选择 0
        AscendC::Select(zLocal, maskLocal, xLocal, static_cast<DTYPE_X>(0),AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE ,this->tileLength);
        outQueueZ.EnQue<DTYPE_Z>(zLocal);
        inQueueX.FreeTensor(xLocal);
        
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.DeQue<DTYPE_Z>();
        AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer0;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuffer1;
    AscendC::GlobalTensor<DTYPE_X> xGm;
    AscendC::GlobalTensor<DTYPE_Z> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t maskSize = 0;
    HardShrinkCustomTilingData tiling;
};

extern "C" __global__ __aicore__ void hard_shrink_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling)
{
    GET_TILING_DATA(tiling_data, tiling);
    KernelHardShrink op;
    op.Init(x, z, tiling_data);
    op.Process();
}
